import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;
// 节点帧动画-切换
@ccclass('AutoFrameAnim')
export class AutoFrameAnim extends Component {
    @property
    spaceTime = 0.1;

    @property
    isRevert = false;

    indx = 0;
    curt = 0;
    direc = 1;

    protected onLoad(): void {
        this.node.children.forEach(e => e.active = false);
    }

    protected onEnable(): void {
        this.indx = 0;
        this.direc = 1;
        this.setFrame();
    }

    update(dt: number) {
        this.curt += dt;
        if (this.curt >= this.spaceTime) {
            this.curt = 0;

            this.indx += this.direc;

            if (this.indx >= this.node.children.length) {
                // 反向播放
                if (this.isRevert) {
                    this.indx = this.node.children.length - 1;
                    this.direc = -1;
                } else {
                    this.indx = 0;
                    this.direc = 1;
                }
            }

            this.setFrame();
        }
    }

    setFrame() {
        for (let i = 0; i < this.node.children.length; i++) {
            this.node.children[i].active = i === this.indx;
        }
    }

}

